package com.sibu.yx.promotion.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.doubo.common.model.SmsStringResult;
import com.doubo.common.model.response.CommonResponse;
import com.doubo.common.util.ExceptionUtil;
import com.doubo.common.util.RandomUtil;
import com.doubo.json.util.JsonUtil;
import com.doubo.redis.util.RedisTemplateUtil;
import com.sibu.yx.promotion.common.constants.AppConstants;
import com.sibu.yx.promotion.common.enums.AliYunSmsTemplateEnum;
import com.sibu.yx.promotion.common.enums.RedisKey;
import com.sibu.yx.promotion.common.manage.ResponseManage;
import com.sibu.yx.promotion.persistence.config.AppConfig;
import com.sibu.yx.promotion.service.SmsService;
import com.sibu.yx.promotion.service.biz.AliyunSmsBiz;

import lombok.extern.slf4j.Slf4j;

/**
 * @author suzheng
 * @date 2021-01-26
 **/
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {


    @Autowired
    private AliyunSmsBiz aliyunSmsBiz;

    @Autowired
    private AppConfig appConfig;

    @Override
    public CommonResponse<String> sendSmsVerifyWithParam(String phone, RedisKey redisKey,
                                                         AliYunSmsTemplateEnum aliyunSmsTemplateEnum,
                                                         String param) {
        if (RedisTemplateUtil.getStr(redisKey.get(phone)) != null) {
            return ResponseManage.fail("验证码已经发送，请勿重复点击");
        }
        String smsCode = RandomUtil.randomNumStringFixLength(6);
        SmsStringResult smsStringResult = aliyunSmsBiz.sendSmsTemplate(aliyunSmsTemplateEnum, phone, smsCode, param);
        if ("ok".equalsIgnoreCase(smsStringResult.getCode())) {
            RedisTemplateUtil.setStr(redisKey.get(phone), smsCode, redisKey.getLiveSecond());
            return ResponseManage.successIfNotNull("发送验证码成功");
        }
        log.error("发送验证码失败:【{}】【{}】", phone, JsonUtil.toJson(smsStringResult));
        return ResponseManage.fail(smsStringResult.getMsg());
    }

    @Override
    public CommonResponse<String> sendSmsVerify(String phone, RedisKey redisKey, AliYunSmsTemplateEnum aliyunSmsTemplateEnum) {
        if (RedisTemplateUtil.getStr(redisKey.get(phone)) != null) {
            return ResponseManage.fail("验证码已经发送，请勿重复点击");
        }

        String smsCode = RandomUtil.randomNumStringFixLength(6);
        SmsStringResult smsStringResult = aliyunSmsBiz.sendSmsTemplate(aliyunSmsTemplateEnum, phone, smsCode);
        if ("ok".equalsIgnoreCase(smsStringResult.getCode())) {
            RedisTemplateUtil.setStr(redisKey.get(phone), smsCode, redisKey.getLiveSecond());
            return ResponseManage.successIfNotNull("发送验证码成功");
        }
        log.error("发送验证码失败:【{}】【{}】", phone, JsonUtil.toJson(smsStringResult));
        return ResponseManage.fail(smsStringResult.getMsg());
    }

    @Override
    public CommonResponse<String> sendSms(String phone, AliYunSmsTemplateEnum aliyunSmsTemplateEnum, String... content) {
        // 下发短信
        try {
            SmsStringResult smsStringResult = aliyunSmsBiz.sendSmsTemplate(aliyunSmsTemplateEnum, phone, content);
            if ("ok".equalsIgnoreCase(smsStringResult.getCode())) {
                return ResponseManage.successIfNotNull("发送短信成功");
            }
            log.error("发送短信失败:【{}】【{}】", phone, JsonUtil.toJson(smsStringResult));
            return ResponseManage.fail("发送短信失败");
        } catch (Exception e) {
            log.error("下发密码短信失败：手机号【{}】,密码【{}】", phone, ExceptionUtil.getAsString(e));
            return ResponseManage.fail("发送失败，请联系客服人员");
        }
    }

    @Override
    public boolean checkSms(String phone, String smsVerify, RedisKey redisKey) {
        if (AppConstants.UNIVERSAL_SMS_VERSIFY_CODE.equals(smsVerify)) {
            log.warn("phone:{}使用了万能验证码", phone);
        }
        //万能验证码
        if ((smsVerify != null && smsVerify.equals(getSmsCode(phone, redisKey)))
                || ("dev".equals(appConfig.getEnvironmentName()) && AppConstants.UNIVERSAL_SMS_VERSIFY_CODE.equals(smsVerify))) {
            RedisTemplateUtil.del(redisKey.get(phone));
            return true;
        }
        return false;
    }

    @Override
    public String getSmsCode(String phone, RedisKey redisKey) {
        return RedisTemplateUtil.getStr(redisKey.get(phone));
    }
}
